package com.verisette.ncos.service.approver.bizunit;

import java.util.List;

import org.apache.log4j.Logger;
import org.corp.agi.common.bean.LabelValueBean;
import org.corp.agi.common.constant.CommonConstant;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.verisette.ncos.persistence.domain.CustomCustomerOrder;
import com.verisette.ncos.persistence.domain.TransCustomerOrder;

public class GetCustomerOrderByIdBU extends CommonBusinessUnit{

	private Logger LOG = Logger.getLogger(GetCustomerOrderByIdBU.class);
	@SuppressWarnings("unchecked")
	@Override
	public void execute(ProcessContext p) throws ProcessException 
	{
		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing GetCustomerOrderByIdBU...");
		}
		
		String id = "";
		if(p.getOriginalRequest().getValueObject() instanceof LabelValueBean){
			id = ((LabelValueBean)p.getOriginalRequest().getValueObject()).getLabel();
		} else if(p.getOriginalRequest().getValueObject() instanceof TransCustomerOrder){
			id = String.valueOf(((TransCustomerOrder)p.getOriginalRequest().getValueObject()).getOrderId());
		} else {
			id = (String)p.getOriginalRequest().getValueObject();
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append("select a.order_id, a.mmb_id, a.mtp_id,")
		.append(" a.status, a.approved_by,a.pdf_path, ")
//		.append(" b.mtp_path, c.mmb_fname_th,c.mmb_mname_th,c.mmb_lname_th, ")
		.append(" b.mtp_path, c.*, ")
		.append(" d.branch_name_th, a.created_date, a.quantity ")
		.append(" from trans_customer_order a, ")
		.append(" master_template b, ")
		.append(" master_member c , ")
		.append(" master_branch d ")
		.append(" where a.mmb_id=c.mmb_id ")
		.append(" and a.mtp_id=b.mtp_id ")
		.append(" and c.branch_id=d.branch_id ")
		.append(" and a.order_id = ?");
		
		//CustomCustomerOrder o = (CustomCustomerOrder)commonDAO.nativeQuerySingleResult(sb.toString());
		List<CustomCustomerOrder> list = (List<CustomCustomerOrder>) commonDAO
				.nativeQuery(sb.toString(), CustomCustomerOrder.class, id);
		
		if(null != list && list.size() == 1)
		{
			p.setValueObject(list.get(0));
			p.setStatus(CommonConstant.SERVICE_STATUS_SUCCESS);
		}
	}

}
